package org.yoki.edu.portal.core.radius.domain;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.BizException;
import lombok.Data;
import org.yoki.edu.portal.core.exception.BasicRestStatus;
import org.yoki.edu.portal.core.radius.infra.RadcheckRepository;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 代码生成器自动生成
 * Date:2017-10-29 17:24:25
 *
 * @author
 */
@Data
@Table(name = "radcheck")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Radcheck implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id; //
    @Column(name = "username")
    private String username; //
    @Column(name = "attribute")
    private String attribute; //
    @Column(name = "op")
    private String op; //
    @Column(name = "value")
    private String value; //

    @Column(name = "loginIp")
    private String loginIp; //
    @Column(name = "loginType")
    private String loginType; //
    @Column(name = "deviceMac")
    private String deviceMac; //
    @Column(name = "openId")
    private String openId; //

    @Column(name = "weChatMark")
    private Boolean weChatMark;

    @Column(name = "createDate")
    private Date createDate; //
    @Column(name = "updateDate")
    private Date updateDate; //

    public Radcheck() {
    }

    public Radcheck(Integer id) {
        setId(id);
    }

    public Serializable getKey() {
        return getId();
    }

    public boolean existed() {
        return false;
    }

    public void save() throws Exception {
        if (existed()) {
            throw new BizException(BasicRestStatus.repeatDataErrorStatus);
        }
        getRadcheckRepository().insert(this);
    }

    public static void delete(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            getRadcheckRepository().deleteByPrimaryKey(id);
        }
    }

    public void update() throws Exception {
        getRadcheckRepository().updateByPrimaryKeySelective(this);
    }

    public Radcheck detail() {
        return getRadcheckRepository().selectByPrimaryKey(getKey());
    }

    public static PageResult<Radcheck> page(PageResult<Radcheck> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<Radcheck> pageInfo = new PageInfo<>(getRadcheckRepository().selectAll());
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());

        return page;
    }

    public void saveOrUpdatePwd() throws Exception {
        Example e = new Example(Radcheck.class);
        if (StringUtil.isEmpty(this.getUsername())) {
            return;
        }
        e.createCriteria().andEqualTo("username", this.getUsername()).andEqualTo("attribute", "Cleartext-Password");
        List<Radcheck> list = getRadcheckRepository().selectByExample(e);
        Date currentDate = new Date();
        if (null != list && list.size() > 0) {
            Radcheck radcheck = list.get(0);
            radcheck.setValue(this.getValue());
            radcheck.setUpdateDate(currentDate);
            radcheck.update();
        } else {
            this.setAttribute("Cleartext-Password");
            this.setOp(":=");
            this.setCreateDate(currentDate);
            this.setUpdateDate(currentDate);
            this.save();
        }
    }

    public void updateIpAndMac() throws Exception {
        Example e = new Example(Radcheck.class);
        if (StringUtil.isEmpty(this.getUsername())) {
            return;
        }
        e.createCriteria().andEqualTo("username", this.getUsername()).andEqualTo("attribute", "Cleartext-Password");
        List<Radcheck> list = getRadcheckRepository().selectByExample(e);
        if (null == list || list.size() <= 0) {
            return;
        }
        Date currentDate = new Date();
        Radcheck radcheck = list.get(0);
        radcheck.setLoginIp(this.getLoginIp());
        radcheck.setDeviceMac(this.getDeviceMac());
        radcheck.setUpdateDate(currentDate);
        radcheck.update();
    }

    public static Radcheck selectTempTypeByUserName(String userName) {
        return selectByUserNameAndType(userName, "temp");
    }

    public static Radcheck selectWeChatTypeByUserName(String userName) {
        return selectByUserNameAndType(userName, "weChat");
    }

    public static Radcheck selectPhoneTypeByUserName(String userName) {
        return selectByUserNameAndType(userName, "phone");
    }

    public static Radcheck selectByUserName(String userName) {
        return selectByUserNameAndType(userName, null);
    }

    private static Radcheck selectByUserNameAndType(String userName, String loginType) {
        Example e = new Example(Radcheck.class);
        if (StringUtil.isEmpty(userName)) {
            return null;
        }
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("username", userName).andEqualTo("attribute", "Cleartext-Password");
        if (!StringUtil.isEmpty(loginType)) {
            criteria.andEqualTo("loginType", loginType);
        }
        List<Radcheck> list = getRadcheckRepository().selectByExample(e);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    public static Radcheck selectTempTypeByLoginIp(String loginIp) {
        return selectByUserNameAndType(loginIp, "temp");
    }

    public static Radcheck selectWeChatTypeByLoginIp(String loginIp) {
        return selectByUserNameAndType(loginIp, "weChat");
    }

    public static Radcheck selectPhoneTypeByLoginIp(String loginIp) {
        return selectByUserNameAndType(loginIp, "phone");
    }

    public static Radcheck selectByLoginIp(String loginIp) {
        return selectByUserNameAndType(loginIp, null);
    }

    private static Radcheck selectByLoginIpAndType(String loginIp, String loginType) {
        Example e = new Example(Radcheck.class);
        if (StringUtil.isEmpty(loginIp)) {
            return null;
        }
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("loginIp", loginIp).andEqualTo("attribute", "Cleartext-Password");
        if (!StringUtil.isEmpty(loginType)) {
            criteria.andEqualTo("loginType", loginType);
        }
        List<Radcheck> list = getRadcheckRepository().selectByExample(e);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 定时清空微信临时账号
     * @param date
     */
    public static void deleteWeChatTempUser(Date date){
        getRadcheckRepository().deleteWeChatTempUser(date);
    }

    /**
     * 定时清空手机临时账号
     * @param date
     */
    public static void deletePhoneTempUser(Date date){
        getRadcheckRepository().deletePhoneTempUser(date);
    }


    private static RadcheckRepository radcheckRepository;

    public static RadcheckRepository getRadcheckRepository() {
        if (null == radcheckRepository) {
            radcheckRepository = InstanceFactory.getInstance(RadcheckRepository.class);
        }
        return radcheckRepository;
    }


}